package com.jbt.mds.obdsdk

import android.app.Activity
import android.content.Context
import android.net.Uri
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide

class ImagePickerAdapter(
    private val mContext: Context,
    private val maxImgCount: Int
) : RecyclerView.Adapter<ImagePickerAdapter.SelectedPicViewHolder>() {

    private val mInflater = LayoutInflater.from(mContext)
    private var mData = mutableListOf<Uri>()
    private var isAdded: Boolean = false // 是否额外添加了最后一个图片
    private var listener: OnRecyclerViewItemClickListener? = null
    private var photoTakenListener: OnPhotoTakenListener? = null

    interface OnRecyclerViewItemClickListener {
        fun onItemClick(view: View, position: Int)
    }

    interface OnPhotoTakenListener {
        fun onPhotoTaken(uri: Uri)
    }

    fun setOnItemClickListener(listener: OnRecyclerViewItemClickListener) {
        this.listener = listener
    }

    fun setOnPhotoTakenListener(listener: OnPhotoTakenListener) {
        this.photoTakenListener = listener
    }

    init {
        setImages(mData)
    }

    fun setImages(data: List<Uri>) {
        mData = data.toMutableList()
        if (itemCount < maxImgCount) {
            mData.add(Uri.EMPTY)
            isAdded = true
        } else {
            isAdded = false
        }
        notifyDataSetChanged()
    }

    fun addImage(uri:Uri) {
        if (isAdded) {
            mData.set(itemCount - 1, uri)
            if (itemCount < maxImgCount) {
                mData.add(Uri.EMPTY)
                isAdded = true
            } else {
                isAdded = false
            }
            notifyItemRangeChanged(itemCount - 2, itemCount)
            
            // 触发拍照完成回调
            photoTakenListener?.onPhotoTaken(uri)
        }
    }

    fun getImages(): List<Uri> {
        // 由于图片未选满时，最后一张显示添加图片，因此这个方法返回真正的已选图片
        return if (isAdded) ArrayList(mData.subList(0, mData.size - 1)) else mData
    }

    // 清空所有图片
    fun clearImages() {
        mData.clear()
        if (itemCount < maxImgCount) {
            mData.add(Uri.EMPTY)
            isAdded = true
        } else {
            isAdded = false
        }
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SelectedPicViewHolder {
        return SelectedPicViewHolder(mInflater.inflate(R.layout.list_item_image, parent, false))
    }

    override fun onBindViewHolder(holder: SelectedPicViewHolder, position: Int) {
        holder.bind(position)
    }

    override fun getItemCount(): Int = mData.size

    inner class SelectedPicViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {
        private val iv_img: ImageView = itemView.findViewById(R.id.iv_img)
        private var clickPosition: Int = 0

        init {
            itemView.setOnClickListener(this)
        }

        fun bind(position: Int) {
            // 根据条目位置设置图片
            val item = mData[position]
            if (isAdded && position == itemCount - 1) {
                iv_img.setImageResource(R.drawable.selector_image_add)
                clickPosition = -1
            } else {
                Glide.with(mContext)
                    .load(item).into(iv_img)
                clickPosition = position
            }
        }

        override fun onClick(v: View) {
            listener?.onItemClick(v, clickPosition)
        }
    }
}